﻿window.$j = window.$J = jQuery;
/*-----------------------jquery plug in-----------------------------------*/

$j.extend($j.event,
{
    stop: function(evt) {
        var _evt = evt || event;
        if (_evt.preventDefault) {
            _evt.preventDefault();
            _evt.stopPropagation();
        }
        else {
            _evt.returnValue = false;
            _evt.cancelBubble = true;
        }
    },
    element: function(evt) {
        var _evt = evt || event;
        return _evt.target || _evt.srcElement;
    }
});
$j.extend({
    pluck: function(array, property) {
        var results = [];
        jQuery.each(array, function(index, value) { results.push(value[property]); });
        return results;
    },
    indexOf: function(array, key) {
        for (var i = 0; i < array.length; i++)
        //if (array[i] == key) return i;
            if (key.indexOf(array[i]) != -1) return i;
        return -1;
    },
    editInPlace: function(id) {
        var package_id = "#__" + id + "__";
        var readOnly_id = "#__" + id + "_ReadOnly__";
        var jid = "#" + id;

        $j(package_id).css("display", "");
        $j(readOnly_id).css("display", "none");
        $j(jid).focus();

        $j(jid).bind("blur", function() {
            $j(package_id).css("display", "none");
            $j(readOnly_id).css("display", "");
            var obj = $j(jid);
            var text = obj.val();
            if (0 == text.length)
                text = "&nbsp;&nbsp;";
            else {
                if (obj.is("select"))
                    text = this.options[this.selectedIndex].text;
            }
            $j(readOnly_id).html(text);
        }
   );
    }
});
/*---------------------------------------------------------------------*/
function $Page$ensure_create_array() {
    try {
        if ($PAGEAJAX.length != 0 && $Page.CallBackControls.length == 0)
            $Page.CallBackControls = jQuery.pluck($PAGEAJAX, "c");
        if ($PAGEAJAX.length != 0 && $Page.UpdateControls.length == 0)
            $Page.UpdateControls = jQuery.pluck($PAGEAJAX, "u");
    }
    catch (e) {
        throw e;
    }
}
/*-----------------------form submit filte-----------------------------*/
function $Page$filter_submit() {
    jQuery("form:first").bind('submit', $Page.FilterSubmitHandle);
}
function $Page$filter_submit_handle(event) {
    $Page.EnsureCreateArray();
    var c = $Page.CallBackControls;
    var u = $Page.UpdateControls;
    var target = $Page.SubmitButtonName;
    $Page.SubmitButtonName = null;
    var _u;
    var iscallback = false;
    var i = jQuery.indexOf(c, target);
    if (-1 != i) {
        iscallback = true;
        $Page.CurrentUpdateControls = u[i];
    }
    if (iscallback) {
        jQuery.event.stop(event);
        $Page.Ajax();
    }
}
/*-----------------------form doPostBack filte-------------------------*/
function $Page$filter_doPostBack() {
    window.__doPostBack = $Page.FilterDoPostBackHandle;
}
function $Page$filter_doPostBack_handle(eventTarget, eventArgument) {
    $Page.EnsureCreateArray();
    jQuery('#__EVENTTARGET').val(eventTarget);
    jQuery('#__EVENTARGUMENT').val(eventArgument);
    var c = $Page.CallBackControls;
    var u = $Page.UpdateControls;
    var target = eventTarget;
   /* var index=target.indexOf('$');
    if(-1 != index)
      target=target.substring(0,index);
      */
    var iscallback = false;
    var i = jQuery.indexOf(c, target);
    var j = jQuery.inArray(target, c);
    if (-1 != i || -1 !=j) {
        iscallback = true;
        $Page.CurrentUpdateControls = u[i];
    }
    if (iscallback) {
        $Page.Ajax();
    }
    else {
        $Page.OriginalDoPostBack(eventTarget, eventArgument);
    }
}
/*-----------------------form click filte-------------------------------*/
function $Page$filter_submit_click() {
    jQuery("form:first").bind('click', $Page.FilterSubmitClickHandle);
}
function $Page$filter_submit_click_handle(event) {
    $Page.SubmitButtonInfo = null;
    $Page.SubmitButtonName = null;
    var e = event;
    var element = jQuery.event.element(e);
    var name = element.name || element.id;
    if (name) {
        if (element.tagName === 'INPUT') {
            var type = element.type;
            if (type === 'submit') {
                $Page.SubmitButtonInfo = name + '=' + encodeURIComponent(element.value);
                $Page.SubmitButtonName = name;
            }
            else if (type === 'image') {
                var x = e.offsetX || e.layerX;
                var y = e.offsetY || e.layerY;
                $Page.SubmitButtonInfo = name + '.x=' + x + '&' + name + '.y=' + y;
                $Page.SubmitButtonName = name;
            }
        }
        else if ((element.tagName === 'BUTTON') && (element.name.length !== 0) && (element.type === 'submit')) {
            $Page.SubmitButtonInfo = name + '=' + encodeURIComponent(element.value);
            $Page.SubmitButtonName = name;
        }
    }
}
/*------------------------------ajax---------------------------------*/
function $Page$ajax() {
    var url = jQuery("form:first").attr("action");
    if (url.indexOf("?") == -1)
        url = url + "?_state=ajax";
    else
        url = url + "&_state=ajax";

    var postData = $("form:first").serialize();
    if (null != $Page.SubmitButtonInfo) {
        if (postData.length > 0)
            postData = postData + "&" + $Page.SubmitButtonInfo;
        else
            postData = $Page.SubmitButtonInfo;
    }
    if (null != $Page.AddPostData) {
        if (postData.length > 0)
            postData = postData + "&" + $Page.AddPostData;
        else
            postData = $Page.AddPostData;
    }

    $Page.IsDisplayLoading = true;
    jQuery.ajax({
        type: "POST",
        url: url,
        data: postData,
        dataType: "text",
        error: $Page.ErrorHandle,
        beforeSend: $Page.AjaxLoading,
        /*complete: $Page.Complete,*/
        success: $Page.AjaxHandle
    }
      );
    $Page.SubmitButtonInfo = null;
}
function $Page$ajaxLoading() {
    window.setTimeout($Page.AjaxLoadingHandle, 10);
}
function $Page$ajaxLoadingHandle() {
    if ($Page.IsDisplayLoading) {
        if ($Page.CurrentUpdateControls.length > 0) {
            jQuery.each($Page.CurrentUpdateControls, function(index, value) {
                if ('' != value) {
                    if (document.getElementById("__" + value + "__") && jQuery("#__" + value + "__").css("display") != "none") {
                        jQuery("#__" + value + "__").block({ message: "<img id=\'_" + value + "loadingimg__\' class=\'loadingimg\' src=\'" + $Page.ResourcesURL + "Images/loading.gif\' />",
                            overlayCSS: { backgroundColor: '#F5F5F5' }, css: { border: 0, backgroundColor: 'transparent' }
                        });
                    }
                    var value_readOnly = value + "_ReadOnly";
                    if (document.getElementById("__" + value + "_ReadOnly__") && jQuery("#__" + value + "_ReadOnly__").css("display") != "none") {
                        jQuery("#__" + value_readOnly + "__").block({ message: "<img id=\'_" + value + "loadingimg__\' class=\'loadingimg\' src=\'" + $Page.ResourcesURL + "Images/loading.gif\' />",
                            overlayCSS: { backgroundColor: '#F5F5F5' }, css: { border: 0, backgroundColor: 'transparent' }
                        });
                    }
                }
            }
         )
        }
    }
    $Page.IsDisplayLoading = false;
}
function $Page$complete(response, str) {
    $Page$completeHandle();
}
function $Page$completeHandle() {
    if ($Page.CurrentUpdateControls.length > 0) {
        jQuery.each($Page.CurrentUpdateControls, function(index, value) {
            jQuery("__" + value + "__").length > 0 ? jQuery("#__" + value + "__").unblock() : null;
            jQuery("__" + value + "_readOnly__").length > 0 ? jQuery("#__" + value_readOnly + "__").unblock() : null;
        }
         )
    }
    $Page.IsDisplayLoading = false;
}
function $Page$ErrorHandle(response, error_type, e_object) {
    $Page$completeHandle();
    alert(error_type + "!  Page error!");
    if ($Page.AjaxDebug) {
        if (0 == jQuery('#AjaxDebug').length) {
            jQuery("<div id='AjaxDebug'></div>").appendTo("body:first");
        }
        jQuery('#AjaxDebug').html(response.responseText);
    }
}
function $Page$ajaxHandle(json_obj) {
    var _obj = eval("("+json_obj+")");//json_obj;
    if (_obj) {
        var _h = eval("(" + _obj.h + ")");
        var _c = eval("(" + _obj.c + ")"); 
        var _js = eval( _obj.js );
        if (_h) {
            for (_name in _h) {
                jQuery("#" + _name).val(_h[_name]);
            }
        }
        if (_c) {
            $Page.IsDisplayLoading = false;
            for (id in _c) {
                if (undefined != eval('window.' + id + '_object')) {
                    var _obj = eval('window.' + id + '_object');
                    _obj.ajaxSuccess(_c[id]);
                }
                else {
                    if (id != '')
                        jQuery("#__" + id + "__").html(_c[id]).change();
                }
            }
        }
        if ('' != _obj.js && null != _obj.js)
            eval(_obj.js);
        $Page.Complete();
        $j.inputInit();
    }
}
function $Page$Invoke(method_name, settings, parameters) {/*parameters is arrary*/
    var url = (settings.url || '' == settings.url) ? jQuery("form:first").attr("action") : settings.url;
    if (url.indexOf("?") == -1)
        url = url + "?_state=ajaxmethod";
    else
        url = url + "&_state=ajaxmethod";
    url += "&methodname=" + encodeURIComponent(method_name) + "&returntype=" + settings.ReturnType || 0;
    if (parameters) {
        var len = parameters.length;
        if (len != 0) {
            for (var i = 0; i < len; i++)
                url += "&methodparameter" + i + "=" + encodeURIComponent(parameters[i]);
        }
    }
    jQuery.ajax({
        type: "GET",
        url: url,
        dataType: "text",
        error: $Page.ErrorHandle,
        beforeSend: settings.BeforeSend || null,
        complete: settings.Complete || null,
        success: settings.Success || null
    }
      );
}
/*------------------------------create $Page----------------------------*/
var $Page = $Page ? $Page : {};
var $page = $Page;
$Page.prototype = {
    initialize: function() { },
    Ajax: function() { },
    AjaxDebug: null,
    IsDisplayLoading: true,
    ResourcesURL: null
}
$Page = {
    pageFilter: function() {
        this.OriginalDoPostBack = window.__doPostBack;
        this.Ajax = $Page$ajax;
        $Page$filter_submit_click();
        $Page$filter_submit();
        $Page$filter_doPostBack();
        this.AjaxDebug = false;
        this.IsDisplayLoading = true;
    },
    OriginalDoPostBack: function(target, argument) { },
    EnsureCreateArray: $Page$ensure_create_array,
    FilterSubmitHandle: $Page$filter_submit_handle,
    FilterDoPostBackHandle: $Page$filter_doPostBack_handle,
    FilterSubmitClickHandle: $Page$filter_submit_click_handle,
    AjaxLoading: $Page$ajaxLoading,
    Complete: $Page$complete,
    AjaxHandle: $Page$ajaxHandle,
    ErrorHandle: $Page$ErrorHandle,
    AjaxLoadingHandle: $Page$ajaxLoadingHandle,
    Invoke: $Page$Invoke,
    CallBackControls: [],
    UpdateControls: [],
    CurrentUpdateControls: [],
    SubmitButtonName: null,
    SubmitButtonInfo: null,
    AddPostData: null
}
/*--------------------------------------------------------------------*/
function ReturnType() { }
ReturnType.OnlyValue = 0;
ReturnType.Array = 1;
ReturnType.JSON = 2;
ReturnType.ArrayInArray = 3;
ReturnType.JSONInArray = 4;

/*----------------------------------input ----------------------------*/
$j.extend({
    inputInit: function() {
        $j("input[type='text'],textarea,input[type='password']").addClass('input_g');
        $j("input[type='text'],textarea,input[type='password']").blur(function() {
            $j(this).removeClass('input_focus');
        });
        $j("input[type='text'],textarea,input[type='password']").focus(function() {
            $j(this).addClass('input_focus'); 
        });
    }
}
);
$j(function() {
    $j.inputInit();
});
/*--------------------------------------------------------------------*/
